'use strict';

angular.
    module('exam').
    component('exam', {
        templateUrl: 'components/exam/exam.template.html',
        controller: function LoginController($scope, $http, $location, $timeout, $injector, $stateParams, $state) {
            var userInfo = JSON.parse(localStorage.getItem('userInfo'));
            var timer = null;
            $scope.showLoading = false;
            $scope.ministryDuty = userInfo.ministryDuty;
            $scope.username = userInfo.username;
            $scope.militaryNum = userInfo.militaryNum;
            $scope.showResult = 0;
            $scope.showSelects = true;

            $scope.showContent = false;
            _initParams();
            function _initParams() {
                $scope.singleAnswer = {
                    value: ""
                };

                $scope.multAnswer = {
                    A: {
                        select: false
                    },
                    B: {
                        select: false
                    },
                    C: {
                        select: false
                    },
                    D: {
                        select: false
                    }
                };
            }
            $scope.$on('destroy', function () {
                if (timer) {
                    clearInterval(timer);
                    timer = null;
                }
                localStorage.clear();
            });

            _initial();

            function _initial() {
                $scope.showLoading = true;
                $http.post('http://192.168.200.196:8900/exam/getCurrentStatus', {
                    ministryDuty: $scope.ministryDuty,
                    username: $scope.username,
                    militaryNum: $scope.militaryNum
                }).then(function (res) {
                    $scope.showLoading = false;
                    res.data.data.timeStr = caleculateTime(res.data.data.remainingTime);
                    $scope.question = res.data.data;
                    parseDate();
                    $scope.showContent = true;
                }, function (error) {
                    $scope.showLoading = false;
                    console.log(error);
                });
            }

            function caleculateTime(mss) {
                var hours = parseInt((mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
                var minutes = parseInt((mss % (1000 * 60 * 60)) / (1000 * 60));
                var seconds = (mss % (1000 * 60)) / 1000;
                return (hours > 9 ? hours : ('0' + hours)) + " : " + (minutes > 9 ? minutes : ('0' + minutes)) + " : " + (seconds > 9 ? seconds : ('0' + seconds));
            }

            function parseDate() {
                if (timer) {
                    clearInterval(timer);
                    timer = null;
                }
                timer = setInterval(function () {
                    $scope.$apply(function () {
                        $scope.question.remainingTime = $scope.question.remainingTime - 1000;
                        $scope.question.timeStr = caleculateTime($scope.question.remainingTime);
                        if ($scope.question.remainingTime <= 0) {
                            clearInterval(timer);
                            $scope.showLoading = true;
                            $http.post('http://192.168.200.196:8900/exam/endExam', {
                                "username": $scope.username,
                                "ministryDuty": $scope.ministryDuty,
                                "militaryNum": $scope.militaryNum
                            }).then(function (res) { 
                                $scope.showLoading = false;
                                $state.go("result", {
                                    score: $scope.question.currentScore
                                });
                            }, function (error) {
                                $scope.showLoading = false;
                            });
                        }
                    });
                }, 1000);
                $scope.bloodStyle = {
                    height: ($scope.question.remainingBlood / $scope.question.totalBlood) * 100 + '%'
                };

            }
            $scope.nextQuestionAction = function () {
                clearInterval(timer);
                $scope.showLoading = true;

                var answer = "";
                if ($scope.question.type == "单选题" || $scope.question.type == "判断题") {
                    answer = $scope.singleAnswer.value;
                } else if ($scope.question.type == "多选题") {
                    var res = [];
                    if ($scope.multAnswer.A.select) {
                        res.push("A");
                    }
                    if ($scope.multAnswer.B.select) {
                        res.push("B");
                    }
                    if ($scope.multAnswer.C.select) {
                        res.push("C");
                    }
                    if ($scope.multAnswer.D.select) {
                        res.push("D");
                    }
                    answer = res.join(",");
                }

                $http.post('http://192.168.200.196:8900/exam/getNextQuestion', {
                    "id": $scope.question.id,
                    "questionId": $scope.question.questionId,
                    "userAnswer": answer,
                    "username": $scope.username,
                    "ministryDuty": $scope.ministryDuty,
                    "currentScore": $scope.question.currentScore,
                    "remainingTime": $scope.question.remainingTime,
                    "totalBlood": $scope.question.totalBlood,
                    "remainingBlood": $scope.question.remainingBlood,
                    "militaryNum": $scope.militaryNum
                }).then(function (res) {
                    $scope.showLoading = false;
                    if (res.data.data.remainingBlood) {
                        res.data.data.timeStr = caleculateTime(res.data.data.remainingTime);
                        if (res.data.data.result == true) {
                            $scope.showResult = 1;
                            playSuccess();
                        } else if (res.data.data.result == false) {
                            playFail();
                            $scope.showResult = -1;
                        }
                        $scope.showSelects = false;

                        setTimeout(() => {
                            $scope.$apply(function () {
                                $scope.showContent = false;
                                $scope.showSelects = true;
                                _initParams();
                                $scope.showResult = 0;
                                $scope.question = res.data.data;
                                parseDate();
                            });
                        }, 800);
                    } else {
                        $state.go("result", {
                            score: res.data.data.currentScore
                        });
                    }
                }, function (error) {
                    $scope.showLoading = false;
                    console.log(error);
                });
            };

            $scope.getCurrentManPosition = function () {
                var res = "";
                switch ($scope.singleAnswer.value) {
                    case "A":
                        res = "A-position-move";
                        break;
                    case "B":
                        res = "B-position-move";
                        break;
                    case "C":
                        res = "C-position-move";
                        break;
                    case "D":
                        res = "D-position-move";
                        break;
                    default:
                        res = "man-position";
                        break;
                }
                return res;
            };

            $scope.getCurrentMulManPosition = function (type) {
                var res = "";
                if ($scope.multAnswer[type].select) {
                    switch (type) {
                        case "A":
                            res = "A-position-move";
                            break;
                        case "B":
                            res = "B-position-move";
                            break;
                        case "C":
                            res = "C-position-move";
                            break;
                        case "D":
                            res = "D-position-move";
                            break;
                    }
                } else {
                    res = "man-position";
                }
                return res;
            };

            $scope.setValue = function (value) {
                $scope.singleAnswer.value = value;
                playJump();
            };

            $scope.setMulValue = function (type) {
                $scope.multAnswer[type].select = !$scope.multAnswer[type].select;
                playJump();
            }

            function playJump(){
                var audio = document.getElementById("jumpAudio");
                audio.load();
                audio.play();
            }
            function playFail(){
                var audio = document.getElementById("failAudio");
                audio.load();
                audio.play();
            }
            function playSuccess(){
                var audio = document.getElementById("successAudio");
                audio.load();
                audio.play();
            }
        }

       
    });